package com.itheima.edu.info.manager.system.service;

import com.itheima.edu.info.manager.system.dao.TeacherDao2;
import com.itheima.edu.info.manager.system.domain.Teacher;

/**
 * 操作老师  业务类
 * 有一个东西他是专门操作数据的
 */
public class TeacherService2 {
   private TeacherDao2 dao = new TeacherDao2();

   /* public TeacherDao getDao() {
        return dao;
    }

    public void setDao(TeacherDao dao) {
        this.dao = dao;
    }*/

    /**
     * service 添加老师操作
     *
     * @param teacher
     * @return
     */
    public boolean addTeacher(Teacher teacher) {
        //创建老师Dao 对象 调用Dao方法
        //TeacherDao dao = new TeacherDao();
        return dao.addTeacher(teacher);

    }

    /**
     * 判断学号是否存在
     *
     * @param id
     */
    public boolean isExits(String id) {
        //查询所有的老师
      Teacher[] stus= dao.findAllTeachers();
        //遍历老师数组 判断是否存在
        boolean flag= false;
        for (int i = 0; i < stus.length; i++) {
            //stus[i]
            Teacher s= stus[i];
            if(s!=null&&s.getId().equals(id)){
                flag=true;
                break;
                //return true;
            }
        }

        return flag;
        //存在 return true
        //不存在return false


    }

    /**
     * service 查询所有老师的方法
     * @return
     */
    public Teacher[] findAllTeachers() {
        //1.dao 调用dao 查询所有老师的方法
        Teacher[] allTeachers = dao.findAllTeachers();

        for (int i = 0; i < allTeachers.length; i++) {
            //allTeachers[i]
            Teacher s= allTeachers[i];
            // //3.不为空 才能返回数组
            if(s!=null){
                return allTeachers;
            }
        }

        //2.判断数组是否为空
        return null;
    }

    /**
     * service  根据学号删除老师信息
     * @param id
     */
    public void deleteTeacherById(String id) {

        dao.deleteById(id);
    }

    public void updateTeacher(String id, Teacher teacher) {
        dao.updateTeacher(id,teacher);

    }
}
